import com.rabbitmq.client.*;
import com.herohead.isdate.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class avamar_gc {

	 public static void main(String[] argv) throws Exception {
		 
		 String hostname = argv[0]; 
		 String QUEUE_NAME = argv[1]; //=="two"
		 String message = argv[2];
		 
		 
		 ConnectionFactory factory = new ConnectionFactory();
		 factory.setHost(hostname);
		 factory.setVirtualHost("backup");
		 factory.setPassword("networker");
		 factory.setUsername("backup");
		 Connection connection = factory.newConnection();
		 Channel channel = connection.createChannel();
		 
		 AMQP.BasicProperties properties = new AMQP.BasicProperties();
		    
		 properties.setDeliveryMode(2);
		 
		 channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		 
		 message = callex("capacity-new.sh --days=40");
		 String rabbitMQ = "";
		 IsDate id = new IsDate();
		 
			for ( String s : message.split("\n")) {
				String[] sline = s.split("\\s+");
				if (id.isDate(sline[0]))
					//rabbitMQ = rabbitMQ + "\n" + s;
						channel.basicPublish("", QUEUE_NAME, properties, s.getBytes());
					}
			
			
		 
		 
		   // int count = 1;
	       // while (count < 2) {
	            //System.out.println("Count is: " + count);
	       //     count++;
	        
	            //String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime());
		    
	            //String message = "Hello World! from java " + timeStamp;
	       //     channel.basicPublish("", QUEUE_NAME, properties, rabbitMQ.getBytes());
	           // channel.basicPublish("", QUEUE_NAME, properties, message.getBytes());
		    
	        //    System.out.println(" [x] Sent '" + rabbitMQ + "'");
	            //Thread.sleep(10);
	        }
		    channel.close();
		    connection.close();
		    System.out.println("Done");
		 
		 
	 }

		public static String callex(String execCommand) throws Exception {
			
			
		try {
			Process p = Runtime.getRuntime().exec(execCommand);
			p.waitFor();
			BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

			String line;
			String line2 = "";
		
			
		
			
			while ((line = input.readLine()) != null) {
				line2 = line2 + "\n" + line;
			}
		
		 return line2;
		
		}catch( IOException ex ){
			return ex.toString();
		
		//Validate the case the file can't be accesed (not enought permissions)

		}
	}
}
